import { generateGridList } from '../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: getApp().globalData.IMG_URL,
    type: -1,
    gridList: generateGridList(10, 2),
    storeGoodsList: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    var pageObj = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function (param) {
      console.log("参数：" + JSON.stringify(param))
      pageObj.setData({ type: param.type })
      wx.setNavigationBarTitle({ title: param.name })
      
      pageObj.storePageForType(pageObj.data.type)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  gotoStoreDetailPage(param: any): void {
    wx.navigateTo({
      url: '/pages/store/storeHome/storeHome',
      ///pages/storeOpenDetail/storeOpenDetail
      events: {
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { storeId: param.currentTarget.dataset.id })
      }
    })
  },
  goodsDetail(param: any): void {
    // console.error(JSON.stringify(param))
    wx.navigateTo({
      url: '/pages/goods/goodsDetail/goodsDetail',
      events: {
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { goodsId: param.currentTarget.dataset.id })
      }
    })
  },
  storePageForType(type: any): void {
    var that = this
    // const token = wx.getStorageSync('token')
    const preUrl = getApp().globalData.SERVER_URL
    const storePageForTypeUrl = preUrl + "fc/store/client/anon/pageForType"
    wx.request({
      url: storePageForTypeUrl,
      method: 'GET',
      data: {
        pageNo: 1,
        pageSize: 5,
        type: type
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        // 'X-Access-Token': token
      },
      success(res: any) {
        wx.stopPullDownRefresh()
        if (res.data.code == 200 && res.data.result.records.length > 0) {
          that.setData({ storeGoodsList: res.data.result.records })
          // console.error(JSON.stringify(that.data.storeGoodsList))
        } else {
          console.info(JSON.stringify(res))
        }
      },
    })
  }
})